<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2025/9/9
  Time: 23:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>我的购物车</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.4.1.min.js"></script>
    <script>
        $(function () {
            $("#all").click(function () {
                $("input[type='checkbox']").prop("checked", $(this).prop("checked"));
            });
            $("#deleteByIds").click(function () {
                // 通过JavaScript代码提交表单
                $("#cartForm").submit();
            });
            $('.modifyCart').click(function () {
                let id = $(this).attr("data-id");
                // let num = $(this).attr("data-num");
                // 根据指定范围匹配指定规则的元素
                // 参数一:匹配规则
                // 参数二:匹配范围,不指定的情况下默认是整个文档
                let num = $("input[type='text']", $(this).parent().parent()).val();
                console.log("id:" + id + ",num:" + num)
                location.href = "${pageContext.request.contextPath}/modify.cart?id=" + id + "&num=" + num;
            });
        })
    </script>
</head>
<body>
<h1>我的购物车</h1>
<hr>
<form id="cartForm" action="${pageContext.request.contextPath}/deleteByIds.cart" method="post">
    <table border="1">
        <tr>
            <th><input type="checkbox" id="all">全选</th>
            <th>序号</th>
            <th>商品名称</th>
            <th>商品数量</th>
            <th>商品总价</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${cart.items}" var="item" varStatus="i">
            <tr>
                <td><input type="checkbox" name="productId" value="${item.product.id}"></td>
                <td>${i.index + 1}</td>
                <td>${item.product.name}</td>
                <td><input type="text" value="${item.num}"></td>
                <td>${item.price}</td>
                <td>
                    <a href="javascript:;" class="modifyCart" data-id="${item.product.id}"
                       data-num="${item.num}">修改</a>
                    <a href="${pageContext.request.contextPath}/deleteById.cart?id=${item.product.id}"
                       onclick="return confirm('是否确认删除')">删除</a>
                </td>
            </tr>
        </c:forEach>
    </table>
</form>
------------------------------------------------------------------<br>
总计:${cart.price}元<br>
<a href="javascript:;" id="deleteByIds">删除选中项</a>
<a href="${pageContext.request.contextPath}/clean.cart">清空购物车</a>
<a href="${pageContext.request.contextPath}/findAll.product">继续购物</a>
<a href="${pageContext.request.contextPath}/jsp/confirm.jsp">结算</a><br>
<span style="color:red;">${cartMsg}</span>
</body>
</html>
